package ezy.lite.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class Network {
    // 没有网络
    public static final int TYPE_INVALID = 0;
    // wap网络，需要配置代理上网
    public static final int TYPE_WAP = 1;
    // 2G网络
    public static final int TYPE_2G = 2;
    // 3G和3G以上网络，统称为快速网络
    public static final int TYPE_3G = 3;
    // wifi网络
    public static final int TYPE_WIFI = 4;

    private static boolean isFastMobileNetwork(int type) {
        switch (type) {
        case TelephonyManager.NETWORK_TYPE_1xRTT:   return false;   // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_CDMA:    return false;   // ~ 14-64 kbps
        case TelephonyManager.NETWORK_TYPE_EDGE:    return false;   // ~ 50-100 kbps
        case TelephonyManager.NETWORK_TYPE_GPRS:    return false;   // ~ 100 kbps
        case TelephonyManager.NETWORK_TYPE_IDEN:    return false;   // ~ 25 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_0:  return true;    // ~ 400-1000 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_A:  return true;    // ~ 600-1400 kbps
        case TelephonyManager.NETWORK_TYPE_EVDO_B:  return true;    // ~ 5 Mbps
        case TelephonyManager.NETWORK_TYPE_HSDPA:   return true;    // ~ 2-14 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPA:    return true;    // ~ 700-1700 kbps
        case TelephonyManager.NETWORK_TYPE_HSUPA:   return true;    // ~ 1-23 Mbps
        case TelephonyManager.NETWORK_TYPE_UMTS:    return true;    // ~ 400-7000 kbps
        case TelephonyManager.NETWORK_TYPE_EHRPD:   return true;    // ~ 1-2 Mbps
        case TelephonyManager.NETWORK_TYPE_HSPAP:   return true;    // ~ 10-20 Mbps
        case TelephonyManager.NETWORK_TYPE_LTE:     return true;    // ~ 10+ Mbps
        case TelephonyManager.NETWORK_TYPE_UNKNOWN: return false;
        default: return false;
        }
    }

    /** 获取网络状态，wifi,wap,2g,3g */
    public static int getNetWorkType(Context context) {

        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivity.getActiveNetworkInfo();

        if (info == null || !info.isConnected()) {
            return TYPE_INVALID;
        }
        if (info.getType() == ConnectivityManager.TYPE_WIFI) {
            return TYPE_WIFI;
        }
        if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
            if (isFastMobileNetwork(info.getSubtype())) {
                return TYPE_3G;
            }
            return TextUtils.isEmpty(System.getProperty("http.proxyHost")) ? TYPE_2G : TYPE_WAP;
        }
        return TYPE_INVALID;
    }

    public static boolean checkNetwork(Context context) {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity == null) {
            return false;
        }
        NetworkInfo[] info = connectivity.getAllNetworkInfo();
        if (info == null) {
            return false;
        }
        for (int i = 0; i < info.length; i++) {
            if (info[i].getState() == NetworkInfo.State.CONNECTED || info[i].getState() == NetworkInfo.State.CONNECTING) {
                return true;
            }
        }
        return false;
    }

    public String ip() {
        try {
            Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                Enumeration<InetAddress> addresses = interfaces.nextElement().getInetAddresses();
                while (addresses.hasMoreElements()) {
                    InetAddress address = addresses.nextElement();
                    if (!address.isLoopbackAddress()) {
                        return address.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return null;
    }
}
